package cn.itcast.servlet;

import cn.itcast.service.CustomerService;
import cn.itcast.vo.Customer;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;

public class AddServlet
  extends HttpServlet
{
  private static final long serialVersionUID = -3623034377180808254L;
  
  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    request.setCharacterEncoding("UTF-8");
    Map<String, String[]> map = request.getParameterMap();
    
    Customer c = new Customer();
    try
    {
      BeanUtils.populate(c, map);
      
      String[] loves = (String[])map.get("love");
      String love = Arrays.toString(loves);
      
      love = love.substring(1, love.length() - 1);
      
      c.setLove(love);
      
      CustomerService cs = new CustomerService();
      int count = cs.save(c);
      if (count == 1) {
        response.sendRedirect(request.getContextPath() + "/listCustomer");
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
  
  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    doGet(request, response);
  }
}
